// import interface.
#include <windows.h>
#include <stdio.h>
#include <assert.h>

char szInterfaceFile[] = "#ifndef %s\r\n"
"#define %s\r\n\r\n"
"// export interface.\r\n"

"#ifdef __cplusplus\r\n"
"extern \"C\" {\r\n"
"#endif\r\n\r\n"
"\r\n"
"#ifdef __cplusplus\r\n"
"}\r\n"
"#endif\r\n\r\n"

"#endif\r\n";

char szSourceFile[] = "// interface of module \'%s\'.\r\n"
"#include \"%s\"\r\n\r\n"
"// import interface.";

#define buf_size 512
int main(int argc, char* argv[]) {
	if (argc != 2) {
		printf("\"Make C Module\" or \"MCM\", create C module file.\r\n\r\n");
		printf("Usage: %s C module name.\r\n", argv[0]);
		return 0;
	}

	{

		FILE* pfInterfaceFile = NULL; // c header file.
		FILE* pfSourceFile = NULL; // c source file.
		char check1[buf_size], check2[buf_size];

		sprintf(check1, "%s.h", argv[1]);
		sprintf(check2, "%s.c", argv[1]);

		if (NULL == (pfInterfaceFile = fopen(check1, "r")) && NULL == (pfSourceFile = fopen(check2, "r"))) {
			char tmp[buf_size];

			pfInterfaceFile = fopen(check1, "wb");
			pfSourceFile =  fopen(check2, "wb");

			{
				char module[buf_size];
				sprintf(module, "%s_h", argv[1]);
				sprintf(tmp, szInterfaceFile, module, module);
				fwrite(tmp, strlen(tmp), 1, pfInterfaceFile);

				sprintf(module, "%s.h", argv[1]);
				sprintf(tmp, szSourceFile, argv[1], module);
				fwrite(tmp, strlen(tmp), 1, pfSourceFile);

				fclose(pfInterfaceFile);
				fclose(pfSourceFile);
			}
		} else {
			printf("C module file \"%s\" already exists.\r\n", argv[1]);
			if (pfInterfaceFile != NULL) {
				fclose(pfInterfaceFile);
			}
			if (pfSourceFile != NULL) {
				fclose(pfSourceFile);
			}
			return 0;
		}
	}
	return 0;
}
